<?php
defined('IN_TS') or die('Access Denied.');

$arrUser = aac('user')->find('user_info', array('userid' => $_SESSION['tsuser']['userid']));
switch ($ts) {
	case "":
		$groupid = $_GET['groupid'];
		$type = $_GET['type'];
		if(!$groupid){
			tsNotice('走错门了！');
		}

		if ($arrUser['isrenzheng']<>1) {
			tsNotice('你还没有取得馆员资格！如果已经通过了考试，请点击<a href="'.tsUrl('user','apply').'">这里</a>申报馆员资格；如果还没有参加培训和考试，请点击<a href="https://shimo.im/docs/fatOK9bmivgLQwuL">这里</a>查看了解详情。');
		}

		$groupUser = aac('group')->find('group_user', array('userid' => $_SESSION['tsuser']['userid'], 'groupid' => $groupid));
		if (!$groupUser) {
			tsNotice('你还不是这个馆的馆员！');
		}

		$strGroup = aac('group')->find('group', array('groupid' => $groupid));


		foreach(array('jpg','jpeg','gif','png') as $v){
			$photoUrl = "uploadfile/idphoto/" . $arrUser['phototime'].$_SESSION['tsuser']['userid'] .'.'.$v;
			if (file_exists($photoUrl)) {
				$photo = 1;
				break;
			}
		}

   		break;

	case "photo":

		$files = $_FILES['file'];

		if($GLOBALS['TS_SITE']['photo_size']){
			$upsize = $GLOBALS['TS_SITE']['photo_size']*1048576;

			if($files ['size']>$upsize){
				tsNotice('上传图片不能超过'.$GLOBALS['TS_SITE']['photo_size'].'M，请修改小点后再上传！');
			}
		}

		$arrType = explode('.', strtolower($files['name']));

     		// 转小写一下
		$type = $arrType[1];

		if (!in_array($type, array('jpg','gif','png','jpeg'))){
			tsNotice('只允许上传jpg/jped/gif/png等类型的附件！');
		}

		$dir ='uploadfile/idphoto';

		if(!is_dir($dir)){
			mkdir($dir,0777);
		}
        $phototime = time();
		$dest =  $dir .'/'.$phototime. $_SESSION['tsuser']['userid'].'.'.$type;
		aac('user')->update('user_info',array('userid'=> $_SESSION['tsuser']['userid']),array('phototime'=>$phototime));

		// 先删除
		foreach(array('jpg','jpeg','gif','png') as $v){
			$file = "uploadfile/idphoto/" .$arrUser['phototime']. $_SESSION['tsuser']['userid'] .'.'.$v;
			if (file_exists($file)) {
				unlink($file);
			}
		}

		// 后上传
		move_uploaded_file($files['tmp_name'], mb_convert_encoding($dest, "gb2312", "UTF-8"));

		chmod($dest, 0777);


		break;

}

include template("cert2");

